package com.phoenix.controller;

import com.phoenix.entity.GroupMember;
import com.phoenix.entity.ResponseData;
import com.phoenix.entity.UserMember;
import com.phoenix.service.GroupMemberService;
import com.phoenix.service.UserMemberService;
import com.phoenix.util.PDFUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PDFController {
    @Autowired
    UserMemberService userMemberService;
    @Autowired
    GroupMemberService groupMemberService;

    public void exportMember(HttpServletResponse response, UserMember userMember) throws UnsupportedEncodingException {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("fill_2", userMember.getUserName());
        dataMap.put("fill_3", userMember.getUserSex());
        dataMap.put("fill_4", userMember.getUserBirthday());
        dataMap.put("fill_5", userMember.getUserEthnicity());
        dataMap.put("fill_6", userMember.getUserParty());
        dataMap.put("fill_7", userMember.getUserWork());
        dataMap.put("fill_8", userMember.getUserPhone());
        dataMap.put("fill_9", userMember.getUserBirthPlace());
        dataMap.put("fill_10", userMember.getUserResume());
        dataMap.put("fill_11", userMember.getReviewComments());

        //图片填充器
        //        Map<String, String> photoMap = new HashMap<>(16);
        //        photoMap.put("logo", "");

        // 设置response参数，可以打开下载页面
        response.reset();
        response.setCharacterEncoding("UTF-8");
        // 定义输出类型
        response.setContentType("application/PDF;charset=utf-8");
        // 设置名称
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(userMember.getUserName() + "_未央区羽毛球协会会员信息登记表.pdf", "UTF-8"));
        try {
            ServletOutputStream out = response.getOutputStream();
            // 模板路径记
            PDFUtil.fillTemplate(dataMap,
//                    photoMap,
                    out, "src/main/resources/pdf/1.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void exportGroup(HttpServletResponse response, GroupMember groupMember) throws UnsupportedEncodingException {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("fill_2", groupMember.getOrgName());
        dataMap.put("fill_3", groupMember.getOrgLoc());
        dataMap.put("fill_4", groupMember.getOrgContact());
        dataMap.put("fill_5", groupMember.getOrgPhone());
        dataMap.put("fill_6", groupMember.getOrgEmail());
        dataMap.put("fill_7", groupMember.getOrgMail());
        dataMap.put("fill_8", groupMember.getOrgResume());
        dataMap.put("fill_9", groupMember.getReviewComments());

        //图片填充器
        //        Map<String, String> photoMap = new HashMap<>(16);
        //        photoMap.put("logo", "");

        // 设置response参数，可以打开下载页面
        response.reset();
        response.setCharacterEncoding("UTF-8");
        // 定义输出类型
        response.setContentType("application/PDF;charset=utf-8");
        // 设置名称
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(groupMember.getOrgName() + "_未央区羽毛球协会团体会员登记表.pdf", "UTF-8"));
        try {
            ServletOutputStream out = response.getOutputStream();
            // 模板路径记
            PDFUtil.fillTemplate(dataMap,
//                    photoMap,
                    out, "src/main/resources/pdf/2.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/exportSingleMember/{infoID}")
    @ResponseBody
    public void exportSingleMember(@PathVariable String infoID) throws UnsupportedEncodingException {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attr.getResponse();
        UserMember userMember = userMemberService.selectMemberByID(infoID);
        PDFController pdfController = new PDFController();
        pdfController.exportMember(response, userMember);
    }

    @RequestMapping("/exportSingleGroup/{groupID}")
    @ResponseBody
    public void exportSingleGroup(@PathVariable String groupID) throws UnsupportedEncodingException {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attr.getResponse();
        GroupMember groupMember = groupMemberService.selectGroupByID(groupID);
        PDFController pdfController = new PDFController();
        if (response != null) {
            pdfController.exportGroup(response, groupMember);
        }
    }

    @RequestMapping("/exportAllMember")
    @ResponseBody
    public ResponseData<String> exportAllMember(@RequestParam String infoID) throws UnsupportedEncodingException {
        String[] IDS = infoID.split(",");
        List<String> IDList = Arrays.asList(IDS);
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attr.getResponse();
        for (String str : IDList) {
            UserMember userMember = userMemberService.selectMemberByID(str);
            PDFController pdfController = new PDFController();
            pdfController.exportMember(response, userMember);
        }
        return new ResponseData<>(200, "批量导出成功！", null);
    }

    @RequestMapping("/exportAllGroup")
    @ResponseBody
    public ResponseData<String> exportAllGroup(@RequestParam String groupID) throws UnsupportedEncodingException {
        String[] IDS = groupID.split(",");
        List<String> IDList = Arrays.asList(IDS);
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attr.getResponse();
        for (String str : IDList) {
            GroupMember groupMember = groupMemberService.selectGroupByID(groupID);
            PDFController pdfController = new PDFController();
            pdfController.exportGroup(response, groupMember);
        }
        return new ResponseData<>(200, "批量导出成功！", null);
    }
}
